function [ bg_models ] = getBackgroundModel(data)
load_settings;
disp('Add bg model: Calculating background model');
fns = cell(size(data)); % number of cameras
bg_models = cell(size(data,1),2);

for ix = 1:size(data,1)
    cf = cell(1);
    for jx = 1:size(data,2)
        
        if ~isempty(data(ix,jx).cam_id)
           cf{jx} = sprintf('%s%08d/%08d_%06d.jpg', image_root,  data(ix,jx).cam_id, data(ix,jx).day, data(ix,jx).hour);
        end
    end
    fns{ix} = cf;
end

for ix = 1:size(data,1)
    disp(sprintf('Calculcating bg model for camera: %d', data(ix,1).cam_id));
    cf = fns{ix};
    im = imread(cf{1}); % Assumes there's at least one image per camera, which is a pretty safe assumption to make, unless something went horribly wrong
    im = rgb2gray(im);
    sizes = cell(1);
    sizes{1} = size(im);
    imStack = zeros([size(im), length(cf)]);
    imStack(:,:,1) = im;
    for jx = 1:length(cf);
            im = imread(cf{jx});
            im = rgb2gray(im);
            sizes{jx} = size(im);
            im = imresize(im, sizes{1});
            imStack(:,:,jx) = im;
    end
    [medianGradX medianGradY] = imageStackMedianGradients(imStack);
    [bg_im, ~]=reconsEdge3(medianGradX, medianGradY);
    bg_models{ix,1} = bg_im;
    bg_models{ix,2} = data(ix,1).cam_id;
end
end